﻿<template>
    <div>
        <el-form label-width="150px" :rules="rules"  :model="AddDepartment" ref="AddDepartment_refs">
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="部门编号" prop="DepartmentNumber">
                        <el-input v-model="AddDepartment.DepartmentNumber"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="10" :offset="1">
                    <el-form-item label="部门名称" prop="DepartmentName">
                        <el-input v-model="AddDepartment.DepartmentName"></el-input>
                        <!--<el-select v-model="Adduser.RoleId" required multiple class="wmax">
                            <el-option value="0" label="全部">全部</el-option>
                            <el-option v-for="(v,k) in AllRoleDB" :value="v.Id" :data-userid="v.Id" :label="v.RoleName" v-text="v.RoleName" />
                        </el-select>-->
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="部门职能" prop="SectoralFunctions">
                        <el-input v-model="AddDepartment.SectoralFunctions"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="10" :offset="1">
                    <el-form-item label="上级部门" prop="SuperiorDepartmentsId">
                        <el-select v-model="AddDepartment.SuperiorDepartmentsId" class="wmax" required filterable placeholder="请选择部门">
                            <el-option value="0" label="请选择部门">请选择部门</el-option>
                            <el-option value="0" label="全部">全部</el-option>
                            <el-option v-for="(v,k) in AllDepartment" :value="v.Id" :data-userid="v.Id" :label="v.DepartmentName" v-text="v.DepartmentName" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="分管领导" prop="LeaderShip">
                        <el-select v-model="AddDepartment.LeaderShip" class="wmax" required filterable placeholder="分管领导">
                            <el-option v-for="v in $store.state.AllUsers" :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="负责人" prop="PartOwner">
                        <el-select v-model="AddDepartment.PartOwner" class="wmax" required filterable placeholder="负责人">
                            <el-option v-for="v in $store.state.AllUsers" :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="副部长" prop="PartOwnerTwo">
                        <el-select v-model="AddDepartment.PartOwnerTwo" class="wmax" required filterable placeholder="副部长">
                            <el-option label="无" :value="0"></el-option>
                            <el-option v-for="v in $store.state.AllUsers" :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="部门类型" prop="DepartType">
                        <el-select v-model="AddDepartment.DepartType" class="wmax" required filterable placeholder="部门类型">
                            <el-option label="服务部门" :value="1">服务部门</el-option>
                            <el-option label="产值部门" :value="2">产值部门</el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="年度产值(元)" prop="YearOutputValue">
                        <el-input v-model.number="AddDepartment.YearOutputValue"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="编码" prop="Code">
                        <el-input v-model="AddDepartment.Code"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="备注" prop="Remarks">
                        <el-input v-model="AddDepartment.Remarks"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col align="center">
                    <el-button type="success" @click="AddDeparmentSave">添加</el-button>
                    <el-button type="success" @click="AddDeparmentreset">重置</el-button>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>
    import * as IsExistjs from "/public/Scriput/IsExist";
    export default {
        name: "AddDepartment",
        props: {
            //人员
            AllUsers: {
                type: Array,
                required: true
            },
            //部门
            AllDepartment: {
                type: Array,
                required: true
            },
        },
        created() {
            IsExistjs.IsExist.http(this.$http, this.$message)
        },
        data() {
            var OutputValue = (rule, value, callback) => {
                if (value.length>0) {
                    return callback(new Error('请输入年度产值(元)'));
                }
                if (!Number.isInteger(value)) {
                    callback(new Error('请输入数字值'));
                } else {
                    callback();
                }
            }
            return {
                rules: {
                    DepartmentNumber: [
                        { required: true, message: '请输入部门编号', trigger: 'blur' },
                    ],
                    DepartmentName: [
                        { required: true, message: '请输入部门名称', trigger: 'blur' },
                    ],
                    SectoralFunctions: [
                        { required: true, message: '请输入部门职能', trigger: 'blur' },
                    ],
                    YearOutputValue: [
                        //{ required: true, message: '请输入年度产值(元)', trigger: 'blur' },
                        { validator: OutputValue, trigger: 'blur' }

                    ],
                    Code: [
                        { required: true, message: '请输入编码', trigger: 'blur' },
                    ],
                },
                AddDepartment: {
                    UserName: '',
                    DepartmentName: '',
                    SectoralFunctions: '',
                    SuperiorDepartmentsId: 0,
                    PartOwner: '',
                    PartOwnerTwo: 0,
                    LeaderShip: '',
                    DepartType: '',
                    YearOutputValue: '',
                    Code: '',
                    Remarks: '',
                },
            }
        },
        methods: {
            AddDeparmentSave() {
                this.$refs.AddDepartment_refs.validate(async (x) => {
                    if (x) {
                        let result = await IsExistjs.IsExist.IsExistDepartmentNumber(this.AddDepartment.DepartmentNumber)
                        if (!result) {
                            let loadding = this.openLoading();
                            this.$http.post("/Department/AddDepartment", { AddDepartment: this.AddDepartment }).then(res => {
                                loadding.close();
                                if (res.data.bit) {
                                    this.$message.success("添加成功");
                                    this.$refs["AddDepartment_refs"].resetFields();
                                    this.$parent.$parent.AddDepartmentShow = false
                                    this.$parent.$parent.GettableDB()
                                } else {
                                    this.$message.error("添加失败");
                                }
                            });
                        } else {
                            this.$message.info('该部门编号已经存在!')
                        }
                    }
                })
            },
            AddDeparmentreset() {
                this.$refs["AddDepartment_refs"].resetFields();
            },
        },

    }
</script>

<style scoped>
    .wmax {
        width: 100%;
    }
</style>
